﻿Public Class PRINTER
    Dim connect As CONDB = CONDB.NewConnection
    Dim PROVIDER_Check As New PROVIDERSCLASS
    Dim s As FILTERCLASS

    '24/7/2558
    'สำหรับตัวแปร prttype ที่เป็นการบอกว่าเครื่องพิมพ์นี้ใช้เอกสารประเภทใด จะไม่มีการเก็บในฐานข้อมูล(ไม่มี master), ใช้วิธีการเซ็ตค่าให้กับ combobox แล้วดึงเอาจาก selectindex + 1, เลือกใช้วิธีนี้เพราะงานเร่งครับ
    'ประเภทเครื่องพิมพ์(prttype) 1 = นำทาง (Slip) 2 = ใบสื่อสาร (Slip) 3 = บัตรนัด (Slip) 4 = หน้าแฟ้ม (Slip) 5 = ใบ request (A5) 6 = ฉลากยา (Lebel) 7 = เอกสาร (A4)  *ดึงมาจากคอมเม้นในtable comtoprn

    Private Sub filterClinic()
        Dim sql As String = "SELECT CAST(clinic AS CHAR(8)) AS 'clinic', clinicname, cliniccode FROM masclinic WHERE status = '1'"
        s = New FILTERCLASS(txtClinic, sql, "id,คลินิก,รหัสคลินิก", "50,160,50", "1,1,1", "1,1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Private Sub filterSoftware()
        Dim sql As String = "SELECT CAST(PRGID AS CHAR(4)) AS PRGID, PRGNAME, PRGCODE FROM masprg WHERE status = 1;"
        s = New FILTERCLASS(txtPrgid, sql, "id software,ชื่อ software,โค้ด software", "100,140,100", "1,1,1", "1,1,1")
        s.SetShowInDown = True
        s.SetShowBorder = True
    End Sub


    Private Sub queryComtoprn()
        Dim objSelectComtoprn As New PRINTERCLASS
        Dim dt As DataTable
        dt = connect.GetTable(objSelectComtoprn.selectPrinter_)
        sgvComtoprn.PrimaryGrid.DataSource = dt
    End Sub

    Private Sub clearForm()
        txtComname.Text = ""
        txtPrtname.Text = ""
        txtClinic.Text = ""
        txtClinic.Tag = ""
        txtPrgid.Text = ""
        cmbPrttype.SelectedIndex = -1
    End Sub

    Private Sub PRINTER_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        filterClinic()
        filterSoftware()
        queryComtoprn()

        DesignForm(Me)
        DesignGrid(sgvComtoprn)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub sgvComtoprn_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles sgvComtoprn.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ctopid").Value() IsNot DBNull.Value Then
            txtCtopid.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ctopid").Value()
        Else
            txtCtopid.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อคอมพิวเตอร์").Value() IsNot DBNull.Value Then
            txtComname.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อคอมพิวเตอร์").Value()
        Else
            txtComname.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อเครื่องพิมพ์").Value() IsNot DBNull.Value Then
            txtPrtname.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อเครื่องพิมพ์").Value()
        Else
            txtPrtname.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสหน่วยงาน").Value() IsNot DBNull.Value Then
            txtClinic.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสหน่วยงาน").Value()
            txtClinic.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("หน่วยงาน").Value()
        Else
            txtClinic.Text = ""
            txtClinic.Tag = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสซอฟต์แวร์").Value() IsNot DBNull.Value Then
            txtPrgid.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสซอฟต์แวร์").Value()
            txtPrgid.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ซอฟต์แวร์").Value()
        Else
            txtPrgid.Text = ""
            txtPrgid.Tag = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("prttype").Value() IsNot DBNull.Value Then
            cmbPrttype.SelectedIndex = Convert.ToInt16(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("prttype").Value()) - 1
        Else
            cmbPrttype.SelectedIndex = -1
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            ckbComtoprn.Checked = True
        Else
            ckbComtoprn.Checked = False
        End If

        btnAddData.Enabled = False
    End Sub

    Private Sub addcomsphere_Click(sender As Object, e As EventArgs) Handles btnAddData.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If txtComname.Text.ToString.Length > 0 And txtPrtname.Text.ToString.Length > 0 And Convert.ToString(txtClinic.Tag) <> "" And Convert.ToString(txtPrgid.Tag) <> "" And cmbPrttype.Text.Length.ToString > 0 Then
                    Dim objInsertData As New PRINTERCLASS

                    objInsertData.COMNAME_ = txtComname.Text
                    objInsertData.PRTNAME_ = txtPrtname.Text
                    objInsertData.CLINIC_ = txtClinic.Tag
                    objInsertData.PRGID_ = txtPrgid.Tag
                    objInsertData.PRTTYPE_ = Convert.ToInt16(cmbPrttype.SelectedIndex) + 1
                    objInsertData.STATUS_ = ckbComtoprn.Checked

                    If objInsertData.insertPrinter() = True Then
                        queryComtoprn()
                        clearForm()
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editcomsphere_Click(sender As Object, e As EventArgs) Handles btnEditData.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If txtComname.Text.ToString.Length > 0 And txtPrtname.Text.ToString.Length > 0 And Convert.ToString(txtClinic.Tag) <> "" And Convert.ToString(txtPrgid.Tag) <> "" And cmbPrttype.Text.Length.ToString > 0 And txtCtopid.Text.Length.ToString > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลเครื่องพิมพ์ดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
                    If editStat = 1 Then
                        Dim objeditData As New PRINTERCLASS

                        objeditData.CTOPID_ = txtCtopid.Text
                        objeditData.COMNAME_ = txtComname.Text
                        objeditData.PRTNAME_ = txtPrtname.Text
                        objeditData.CLINIC_ = txtClinic.Tag
                        objeditData.PRGID_ = txtPrgid.Tag
                        objeditData.PRTTYPE_ = Convert.ToInt16(cmbPrttype.SelectedIndex) + 1
                        objeditData.STATUS_ = ckbComtoprn.Checked

                        If objeditData.editPrinter() = True Then
                            queryComtoprn()
                            clearForm()
                        End If
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If
                btnAddData.Enabled = True
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL("comtoprn")
        nextform.ShowDialog()
    End Sub

    Private Sub exportExcel_Click(sender As Object, e As EventArgs) Handles exportExcel.Click
        Dim nextform As New IMPORTEXCEL("comtoprn")
        nextform.ShowDialog()
    End Sub
End Class